package com.ktjiaoyu.news.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.news.entity.User;
import com.ktjiaoyu.news.service.UserServiceDao;
import com.ktjiaoyu.news.service.impl.UserServiceDaoImpl;
import com.ktjiaoyu.news.utils.Page;
import com.ktjiaoyu.news.utils.R;
import com.ktjiaoyu.news.utils.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "UserServlet2", urlPatterns = "/api/admin/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        UserServiceDao userServiceDao=new UserServiceDaoImpl();
        //根据这个判断执行那个方法
        String opr=req.getParameter("opr");
        //获取用户信息
        Long id=RequestUtil.parseLong(req,"id",null);
        String roleId=req.getParameter("roleId");
        String loginName=req.getParameter("loginName");
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        String sex=req.getParameter("sex");
        String identityCode=req.getParameter("identityCode");
        String email=req.getParameter("email");
        String mobile=req.getParameter("mobile");
        //创建用户对象
        User user=new User();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setIdentityCode(identityCode);
        user.setEmail(email);
        user.setUserName(userName);
        user.setMobile(mobile);
        //返回JSON对象
        R result=null;
        if("page".equals(opr)){
            // 分页查询
            Integer pageNo = RequestUtil.parseInt(req, "pageNo", 1);
            Integer pageSize = RequestUtil.parseInt(req, "pageSize", 10);
            Page<User> page = userServiceDao.getPage(user, pageNo, pageSize);
            // 返回分页数据
            result = R.ok(page);
        }else if("list".equals(opr)){
            // 列表全部数据
            List<User> list = userServiceDao.getAll();
            // 返回所有数据
            result = R.ok(list);
        }else if("save".equals(opr)){
            if(sex!=null) {
                user.setSex(Integer.parseInt(req.getParameter("sex")));
            }
            if(id!=null){
                user.setId(Integer.valueOf(String.valueOf(id)));
            }
            if(roleId!=null){
                user.setRoleId(Integer.valueOf(roleId));
            }
            // 新增或修改数据，统一使用save
            boolean save = userServiceDao.update(user);
            if(save){
                result = R.ok(user);
            }else{
                result = R.fail(user);
            }
        }else if("detail".equals(opr)){
            // 获取详情数据
            user = userServiceDao.getByIdUser(Integer.parseInt(String.valueOf(id)));
            if(user != null){
                result = R.ok(user);
            }else{
                result = R.fail();
            }
        }else if("del".equals(opr)){
            // 删除单条数据
            boolean deleted = userServiceDao.deleteUser(Integer.parseInt(String.valueOf(id)));
            if(deleted){
                result = R.ok(id);
            }else{
                result = R.fail(id);
            }
        }
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
